package doc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import model.Directory;
import model.User;
import model.UserFile;
import myutil.DB;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport
{
	private String username, email, password, password2;

	private ResultSet rs;
	private Connection conn;
	private String sqlQuery1, sqlQuery2;
	int rows, newGeneratedUId;
	private Statement stmt;
	private Map session;

	@SuppressWarnings("unchecked")
	@Override
	public String execute()
	{
		float usage;
		int percent;
		int userRootDir;
		session = ActionContext.getContext().getSession();
		conn = DB.getConnection();
		System.out.println("Request for registering a account recieved");
		if (conn == null)
		{
			return ERROR;
		}
		if (username == null || username.isEmpty())
		{
			addFieldError(username, "Username can not be empty");
			return ERROR;
		}
		if (password == null || password.isEmpty())
		{
			addFieldError(password, "Password can not be empty");
			return ERROR;
		}
		if (email == null || email.isEmpty())
		{
			addFieldError(email, "Email can not be empty");
			return ERROR;
		}
		if (password2 == null || password != password2)
		{
			addFieldError(password2, "Password provided do not match");
			return ERROR;
		}
		if ((User.findExistingUser(username)).equals("Exists"))
		{
			addFieldError(username, "Username is not available");
			return ERROR;
		}
		System.out.println("being inserted: " + username + " " + password + " " + email + " 000000000000000000000000000000000000000");
		sqlQuery1 = "insert into users (username,password,emailid) values ('" + username + "','" + password + "','" + email + "')";

		try
		{
			stmt = conn.createStatement();
			rows = stmt.executeUpdate(sqlQuery1, Statement.RETURN_GENERATED_KEYS); // by this method we retrieve user id that has been auto generated by mysql.
			rs = stmt.getGeneratedKeys();
			if (rs.next())
			{
				newGeneratedUId = rs.getInt(1);
			}
			sqlQuery2 = "insert into user_folders(foldername,userid) values('root', " + newGeneratedUId + " ) ";
			rows = stmt.executeUpdate(sqlQuery2);
			session.put("userkey", username);
			session.put("uid", newGeneratedUId);
			userRootDir = Directory.getUserRootDir(newGeneratedUId);
			session.put("userrootdirid", userRootDir);
			session.put("usercurrentdirid", userRootDir);
			usage = UserFile.getUserUsage(newGeneratedUId);
			session.put("usage", usage);
			percent = (int) (usage * 2);
			session.put("percent", percent);
			return SUCCESS;
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return ERROR;
		}
		finally
		{

			try
			{
				conn.close();
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public String getPassword2()
	{
		return password2;
	}

	public void setPassword2(String password2)
	{
		this.password2 = password2;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

}
